﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Cvk.News;

namespace BatDongSan.Modules
{
    public partial class Uct_News_Detail : System.Web.UI.UserControl
    {
        public static int _idParent, _idNews;
        protected void Page_Load(object sender, EventArgs e)
        {

        }
        public void LoadNav(int catId)
        {
            try
            {
                int parentId = NewsBll.claNewsType.GetParentId(catId, Utility.strConnect);
                if (parentId > 0)
                {
                    ltrnav.Text = string.Format("<div class=\"navlink\"><a href=\"/News.aspx?idt={0}\">{1}</a></div>", parentId, NewsBll.claNewsTypeDetail.GetTypeName(1, parentId, Utility.strConnect));
                    ltrnav.Text += "<span>></span>";
                    ltrnav.Text += string.Format("<div class=\"navlink\"><a href=\"/News.aspx?idt={0}\">{1}</a></div>", parentId, NewsBll.claNewsTypeDetail.GetTypeName(1, catId, Utility.strConnect));
                }
                else { ltrnav.Text = string.Format("<div class=\"navlink\"><a href=\"/News.aspx?idt={0}\">{1}</a></div>", parentId, NewsBll.claNewsTypeDetail.GetTypeName(1, catId, Utility.strConnect)); }
            }
            catch { }
        }
        public void LoadInfo(int idParent, int idNews)
        {
            try
            {
                LoadNav(idParent);
                _idParent = idParent;
                _idNews = idNews;
                //lnkbtTitle.PostBackUrl = string.Format("~/NewsChild.aspx?idt={0}", _idParent);
                //lnkbtTitle.PostBackUrl = string.Format("http://{0}/{1}.htm", Request.Url.Authority, NewsBll.claNewsTypeDetail.GetUrlFriendly(1, _idParent, Utility.strConnect));
                //lblTitle.Text = NewsBll.claNewsTypeDetail.GetTypeName(1, _idParent, Utility.strConnect).ToUpper();
                if (idNews > 0)
                {
                    VG_New info = NewsBll.claNews.GetNews(1, idParent, idNews, false, Utility.strConnect); ;
                    if (info != null)
                    {
                        lblDate.Text = Convert.ToDateTime(info.PostDate).ToString("dd/MM/yyyy, hh:mm tt ('GMT'+7)");
                        lblSubject.Text = info.Subject;
                        lblContent.Text = info.Content;

                        NewsBll.claNews.UpdateView(idNews, Utility.strConnect);
                    }
                }
                LoadNewsOrder(idParent);
            }
            catch { }
        }
        public void LoadNewsOrder(int ParentId)
        {
            try
            {
                List<VG_New> list = NewsBll.claNews.GetListNewsByParent(1, ParentId, (pager1.CurrentIndex - 1) * pager1.PageSize, pager1.PageSize, true, Utility.strConnect);
                try
                {
                    list.Remove(list.Single(p => p.NewsId == _idNews));
                }
                catch { }
                rptNewsOther.DataSource = list;
                rptNewsOther.DataBind();

                Int32 _totalRecords = NewsBll.claNews.GetListNewsByParentCount(1, ParentId, true, Utility.strConnect);
                pager1.ItemCount = _totalRecords;

                if (_totalRecords > pager1.PageSize)
                    pager1.Visible = true;
                else
                    pager1.Visible = false;
            }
            catch { }
        }
        protected void mktpage_Command(object sender, CommandEventArgs e)
        {
            try
            {
                int currnetPageIndx = Convert.ToInt32(e.CommandArgument);
                pager1.CurrentIndex = currnetPageIndx;
                LoadNewsOrder(_idParent);
            }
            catch { }
        }
        protected void rptNewsOther_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            try
            {
                if (e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType == ListItemType.Item)
                {
                    VG_New info = (VG_New)e.Item.DataItem;
                    HyperLink hplSubject = (HyperLink)e.Item.FindControl("hplSubject");
                    hplSubject.Text = info.Subject;
                    hplSubject.NavigateUrl = string.Format("/NewsDetail.aspx?idt={0}&id={1}", info.NewsTypeId, info.NewsId);
                    //hplSubject.NavigateUrl = string.Format("http://{0}/{1}/{2}/{3}.htm", Request.Url.Authority, NewsBll.claNewsTypeDetail.GetUrlFriendly(1, info.NewsTypeId, Utility.strConnect), info.NewsId, info.SeoUrl);
                }
            }
            catch { }
        }
    }
}